<?php

class Document_types_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = 'set_document_types';
        $this->load->model("functions_model");
    }

    function newDocType($added_by, $parent, $name) {
        $data['parent'] = $parent;
        $data['name'] = $name;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }
    function updateDocType($tid, $name) {
        $whe['tid'] = $tid;
        $data['name'] = $name;
        $this->db->update($this->tableName, $data,$whe);
    }
    function deleteDocType($tid) {
        $data['parent'] = $tid;
        $this->db->delete($this->tableName, $data);
        $data=array();
        $data['tid'] = $tid;
        $this->db->delete($this->tableName, $data);
    }

    function getAll($parent=NULL,$tid=NULL) {
        if ($parent != NULL) {
            $data['parent'] = $parent;
            $this->db->where($data);
        }
        if ($tid != NULL) {
            $data['tid'] = $tid;
            $this->db->where($data);
        }
        $this->db->select('tid,added_by, name,parent,date_added');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $row['name']=$row['name'];
            $result[] = $row;
        }
        return $result;
    }

}

?>
